﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyOA.Model;
using System.Data;
using System.Data.SqlClient;

namespace MyOA.DAL
{
    public class UserInfoService
    {
        DBHelper db = new DBHelper();
        RoleInfoService roleInfoService = new RoleInfoService();
        UserStateService userStateService = new UserStateService();
        DepartInfoService departInfoService = new DepartInfoService();
        //查找所有UserInfo信息
        public IList<UserInfo> GetAllUserInfo()
        {
            List<UserInfo> userInfoList = new List<UserInfo>();
            using (DataTable table = db.getDataTable("proc_getAllUserInfo"))
            {
                foreach (DataRow row in table.Rows)
                {
                    UserInfo userInfo = new UserInfo();
                    userInfo.UserId = (int)row["UserId"];
                    userInfo.LoginId = (string)row["LoginId"];
                    userInfo.Password = (string)row["Password"];
                    userInfo.UserName = (string)row["UserName"];
                    userInfo.Gender = (int)row["Gender"];
                    int roleId = (int)row["RoleId"];
                    userInfo.Role = roleInfoService.GetRoleInfoByRoleId(roleId);
                    int userStateId = (int)row["UserStateId"];
                    userInfo.UserState = userStateService.getUserStateByUserStateId(userStateId);
                    int departId = (int)row["DepartId"];
                    userInfo.Depart = departInfoService.GetDepartInfoBuDepartInfoByDepartId(departId);
                    userInfoList.Add(userInfo);
                }
                db.CloseConnection();
                return userInfoList;
            }
        }
        //根据userId查找UserInfo信息
        public UserInfo GetUserInfoByUserId(int userId)
        {
            DBHelper db = new DBHelper();
            try
            {
                using (SqlDataReader reader = db.getDataReader("proc_getUserInfoByUserId", new SqlParameter("@userId", userId)))
                {
                    UserInfo userInfo = new UserInfo();
                    if (reader.Read())
                    {
                        userInfo.UserId = (int)reader["UserId"];
                        userInfo.LoginId = (string)reader["LoginId"];
                        userInfo.Password = (string)reader["Password"];
                        userInfo.UserName = (string)reader["UserName"];
                        userInfo.Gender = (int)reader["Gender"];
                        userInfo.Role = roleInfoService.GetRoleInfoByRoleId((int)reader["RoleId"]);
                        userInfo.UserState = userStateService.getUserStateByUserStateId((int)reader["UserStateId"]);
                        int departId = (int)reader["DepartId"];
                        userInfo.Depart = departInfoService.GetDepartInfoBuDepartInfoByDepartId(departId);
                    }
                    reader.Close();
                    db.CloseConnection();
                    return userInfo;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }

        }
        //*根据DepartId查找UserInfo
        public int GetCountUserInfoByDepartId(int departId)
        {
            try
            {
                int result = Convert.ToInt32(db.getScalar("proc_getCountUserInfoByDepartId", new SqlParameter("@DepartId", departId)));
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        //根据LoginId查找UserInfo信息
        public UserInfo GetUserInfoByLoginId(string loginId)
        {
            try
            {
                using (SqlDataReader reader = db.getDataReader("proc_getUserInfoByLoginId", new SqlParameter("@LoginId", loginId)))
                {
                    UserInfo userInfo = new UserInfo();
                    if (reader.Read())
                    {
                        userInfo.UserId = (int)reader["UserId"];
                        userInfo.LoginId = (string)reader["LoginId"];
                        userInfo.Password = (string)reader["Password"];
                        userInfo.UserName = (string)reader["UserName"];
                        userInfo.Gender = (int)reader["Gender"];
                        userInfo.Role = roleInfoService.GetRoleInfoByRoleId((int)reader["RoleId"]);
                        userInfo.UserState = userStateService.getUserStateByUserStateId((int)reader["UserStateId"]);
                        int departId = (int)reader["DepartId"];
                        userInfo.Depart = departInfoService.GetDepartInfoBuDepartInfoByDepartId(departId);

                    }
                    reader.Close();
                    db.CloseConnection();
                    return userInfo;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }

        }
        //根据UserId修改UserInfo信息
        public int UpdateUserInfoByUserId(UserInfo userInfo)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[]
               { 
                   new SqlParameter("@UserId",userInfo.UserId),
                   new SqlParameter("@LoginId",userInfo.LoginId),
                   new SqlParameter("@UserName",userInfo.UserName),
                   new SqlParameter("@PassWord",userInfo.Password),
                   new SqlParameter("@DepartId",userInfo.Depart.DepartId),
                   new SqlParameter("@Gender",userInfo.Gender),
                   //new SqlParameter("@RoleId",userInfo.Role.RoleId),
                  // new SqlParameter("@UserStateId",userInfo.UserState.UserStateId)
               };
                int result = db.getCount("proc_updateUserInfoByUserId", para);
                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw ex;
            }
        }
        //根据UserId删除UserInfo信息
        public int DeleteUserInfoByuserId(int userId)
        {
            try
            {
                int result = db.getCount("proc_deleteUserInfoByUserId", new SqlParameter("@UserId", userId));
                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }
        //添加userInfo信息
        public int AddUserInfo(UserInfo userInfo)
        {
            try
            {
                SqlParameter[] para = new SqlParameter[]
               { 
                   new SqlParameter("@LoginId",userInfo.LoginId),
                   new SqlParameter("@UserName",userInfo.UserName),
                   new SqlParameter("@PassWord",userInfo.Password),
                   new SqlParameter("@DepartId",userInfo.Depart.DepartId),
                   new SqlParameter("@Gender",userInfo.Gender),
                   new SqlParameter("@RoleId",userInfo.Role.RoleId),
                   new SqlParameter("@UserStateId",userInfo.UserState.UserStateId)
               };
                int result = db.getCount("proc_insertUserInfo", para);
                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 根据messageId查找所有收信人
        /// </summary>
        /// <param name="messageId">收信人收到的信息Id</param>
        /// <returns>所有收信人</returns>
        public IList<UserInfo> getAllUserInfoByMessageId(int messageId)
        {
            try
            {
                List<UserInfo> userInfoList = new List<UserInfo>();
                using (DataTable table = db.getDataTable("proc_getAllUserInfoByMessageId", new SqlParameter("@messageId", messageId)))
                {
                    foreach (DataRow row in table.Rows)
                    {
                        UserInfo userInfo = new UserInfo();
                        userInfo.UserId = (int)row["UserId"];
                        userInfo.LoginId = (string)row["LoginId"];
                        userInfo.Password = (string)row["Password"];
                        userInfo.UserName = (string)row["UserName"];
                        userInfo.Gender = (int)row["Gender"];
                        int roleId = (int)row["RoleId"];
                        userInfo.Role = roleInfoService.GetRoleInfoByRoleId(roleId);
                        int userStateId = (int)row["UserStateId"];
                        userInfo.UserState = userStateService.getUserStateByUserStateId(userStateId);
                        int departID = (int)row["DepartId"];
                        userInfo.Depart = departInfoService.GetDepartInfoBuDepartInfoByDepartId(departID);
                        userInfoList.Add(userInfo);
                    }
                    db.CloseConnection();
                    return userInfoList;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }
        /// <summary>
        /// 根据departId查找该机构的所有用户
        /// </summary>
        /// <param name="departId">机构Id</param>
        /// <returns>用户集合</returns>
        public IList<UserInfo> getAllUserInfoByDepartId(int departId)
        {
            try
            {
                List<UserInfo> userInfoList = new List<UserInfo>();
                using (DataTable table = db.getDataTable("proc_getAllUserInfoByDepartId", new SqlParameter("@departId", departId)))
                {
                    foreach (DataRow row in table.Rows)
                    {
                        UserInfo userInfo = new UserInfo();
                        userInfo.UserId = (int)row["UserId"];
                        userInfo.LoginId = (string)row["LoginId"];
                        userInfo.Password = (string)row["Password"];
                        userInfo.UserName = (string)row["UserName"];
                        userInfo.Gender = (int)row["Gender"];
                        int roleId = (int)row["RoleId"];
                        userInfo.Role = roleInfoService.GetRoleInfoByRoleId(roleId);
                        int userStateId = (int)row["UserStateId"];
                        userInfo.UserState = userStateService.getUserStateByUserStateId(userStateId);
                        int departID = (int)row["DepartId"];
                        userInfo.Depart = departInfoService.GetDepartInfoBuDepartInfoByDepartId(departID);
                        userInfoList.Add(userInfo);
                    }
                    db.CloseConnection();
                    return userInfoList;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }
        }

        //根据UserName查找UserInfo信息
        public UserInfo GetUserInfoByUserName(string userName)
        {
            using (SqlDataReader reader = db.getDataReader("proc_getUserInfoByUserName", new SqlParameter("@UserName", userName)))
            {
                UserInfo userInfo = new UserInfo();
                if (reader.Read())
                {
                    userInfo.UserId = (int)reader["UserId"];
                    userInfo.LoginId = (string)reader["LoginId"];
                    userInfo.Password = (string)reader["Password"];
                    userInfo.UserName = (string)reader["UserName"];
                    userInfo.Gender = (int)reader["Gender"];
                    //userInfo.Role = roleInfoService.GetRoleInfoByRoleId((int)reader["RoleId"]);
                    // userInfo.UserState = userStateService.getUserStateByUserStateId((int)reader["UserStateId"]);
                    int departId = (int)reader["DepartId"];
                    userInfo.Depart = departInfoService.GetDepartInfoBuDepartInfoByDepartId(departId);
                    reader.Close();
                }
                db.CloseConnection();
                return userInfo;
            }
        }
    }
}
